package eg.edu.eelu.fyp2013.jdetector.core.input_output;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.imageio.ImageIO;

public class LoadData {
	
	
	public String imageFileName; 
	
	public AMatrix [] loadNormallearningdata (String filename , int selectedvalue)
	{
		
		
		
		final File dir = new File(filename);
		
	    // array of supported extensions (use a List if you prefer)
	    final String[] EXTENSIONS = new String[]{
	        "gif", "png", "bmp", "jpg" };// and other formats you need
	    
	    
	     final FilenameFilter IMAGE_FILTER = new FilenameFilter() {

	        @Override
	        public boolean accept(final File dir, final String name) {
	            for (final String ext : EXTENSIONS) {
	                if (name.endsWith("." + ext)) {
	                    return (true);
	                }
	            }
	            return (false);
	        }
	    };
	    AMatrix [] Normaldataset = new AMatrix [dir.listFiles().length];
	   
	    if (dir.isDirectory()) { // make sure it's a directory
	    	
	    	int count = 0;
      		BufferedImage img = null;
            for (final File f : dir.listFiles(IMAGE_FILTER)) {
            	
     
                double [][] im; 
                try {
                    img = ImageIO.read(f);
                    imageFileName = f.getName();
                    im = new double [img.getWidth()][img.getHeight()];
                    for (int i = 0; i < img.getWidth(); i++){
                    	for(int j =0; j< img.getHeight(); j++){
                    		im[i][j] = (double)img.getRGB(i, j);
                    	}
                    }
                    Normaldataset[count].GetImage = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_INT_RGB);
                    Normaldataset[count].GetImage = img;
                    Normaldataset[count] = new AMatrix();
                    Normaldataset[count].matrix = im;
                    Normaldataset[count].label = selectedvalue;
                    Normaldataset[count].width = img.getWidth();
                    Normaldataset[count].height = img.getHeight();
                    count++;
                   
                } catch (final IOException e) {
                    // handle errors here
                }
            }
        }
	    	
		return Normaldataset;
		
	}
	public AMatrix [] loadabNormallearningdata (String filename , int selectedvalue)
	{
		
		final File dir = new File(filename);

	    // array of supported extensions (use a List if you prefer)
	    final String[] EXTENSIONS = new String[]{
	        "gif", "png", "bmp", "jpg" };// and other formats you need
	    
	    
	     final FilenameFilter IMAGE_FILTER = new FilenameFilter() {

	        @Override
	        public boolean accept(final File dir, final String name) {
	            for (final String ext : EXTENSIONS) {
	                if (name.endsWith("." + ext)) {
	                    return (true);
	                }
	            }
	            return (false);
	        }
	    };
	    AMatrix [] AbNormaldataset = new AMatrix [dir.listFiles().length];
	   
	    if (dir.isDirectory()) { // make sure it's a directory
	    	
	    	int count = 0;
	    	
            for (final File f : dir.listFiles(IMAGE_FILTER)) {
            	
            	 
                BufferedImage img = null;
                double [][] im; 
                try {
                    img = ImageIO.read(f);
                    im = new double [img.getWidth()][img.getHeight()];
                    for (int i = 0; i < img.getWidth(); i++){
                    	for(int j =0; j< img.getHeight(); j++){
                    		im[i][j] = (double)img.getRGB(i, j);
                    	}
                    }
                    AbNormaldataset[count] = new AMatrix();
                    AbNormaldataset[count].matrix = im;
                    AbNormaldataset[count].GetImage = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_INT_RGB);
                    AbNormaldataset[count].GetImage = img;
                    AbNormaldataset[count].label = selectedvalue;
                    AbNormaldataset[count].width = img.getWidth();
                    AbNormaldataset[count].height = img.getHeight();
                    count++;
                } catch (final IOException e) {
                    // handle errors here
                }
            }
        }
	    	
		return AbNormaldataset;
		
	}
	
	// Load data for user interface from folder
	
	public AMatrix [] loadInputdata (String filename)
	{
		
		final File dir = new File(filename);

	    // array of supported extensions (use a List if you prefer)
	    final String[] EXTENSIONS = new String[]{
	        "gif", "png", "bmp", "jpg" };// and other formats you need
	    
	    
	     final FilenameFilter IMAGE_FILTER = new FilenameFilter() {

	        @Override
	        public boolean accept(final File dir, final String name) {
	            for (final String ext : EXTENSIONS) {
	                if (name.endsWith("." + ext)) {
	                    return (true);
	                }
	            }
	            return (false);
	        }
	    };
	    AMatrix [] inputData = new AMatrix [dir.listFiles().length];
	   
	    if (dir.isDirectory()) { // make sure it's a directory
	    	
	    	int count = 0;
	    	
            for (final File f : dir.listFiles(IMAGE_FILTER)) {
            	
            	 
                BufferedImage img = null;
                double [][] im; 
                try {
                    img = ImageIO.read(f);
                    im = new double [img.getWidth()][img.getHeight()];
                    for (int i = 0; i < img.getWidth(); i++){
                    	for(int j =0; j< img.getHeight(); j++){
                    		im[i][j] = (double)img.getRGB(i, j);
                    	}
                    }
                    inputData[count] = new AMatrix();
                    inputData[count].matrix = im;
                    inputData[count].GetImage = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_INT_RGB);
                    inputData[count].GetImage = img;
                    inputData[count].width = img.getWidth();
                    inputData[count].height = img.getHeight();
                    count++;
                } catch (final IOException e) {
                    // handle errors here
                }
            }
        }
	    	
		return inputData;
		
	}
	
	
    
}
